//
//  GameScreen.h
//  match2
//
//  Created by Tri Vo on 7/13/13.
//
//

#ifndef __match2__GameScreen__
#define __match2__GameScreen__

#include <iostream>
#include <memory.h>
#include "GameController.h"
#include <vector.h>
#include "CTile.h"
class GameScreen : public cc::CCLayer {
private:
    // Variables
    int mBeginX;
    int mBeginY;
    cc::CCSize mTileSize;
    std::vector<CTile> *pmListTile;
    std::vector<cc::CCSprite> *pmListSprite;
    cc::CCSpriteBatchNode *pmBatchNode;
    cc::CCArray         *pmArray;
    cc::CCArray         *pmArraySelectedNumber;
    cc::CCSpriteFrameCache *pmCache;
    cc::CCAnimation *pmAnimation;
    cc::CCSprite *pmSprite;
    cc::CCMenu *pmMenuRow_1;
    cc::CCMenu *pmMenuRow_2;
    cc::CCMenu *pmMenuRow_3;
    cc::CCMenu *pmMenuRow_4;
    int mMatrix[4][5];
    cc::CCMenuItemSprite *pmPreviousSelectedItem;
    cc::CCLabelTTF *pmLabelMoveCount;
    int pmMoveCount;
    void createMatrix() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                
                bool isExisted = false;
                do {
                    int randNumber = rand()%10 + 1;
                    if (!checkNumberExists(randNumber)) {
                        mMatrix[i][j] = randNumber;
                        isExisted = true;
                    }
                } while (!isExisted);
            }
        }
    }
    
    void clearContents() {
        pmPreviousSelectedItem = NULL;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                mMatrix[i][j] = 0;
            }
        }
        if(pmArraySelectedNumber != NULL) {
            pmArraySelectedNumber->removeAllObjects();
        }
        
        if(pmMenuRow_1 != NULL) {
            if (pmMenuRow_1->getChildren()->count() >0) {
                pmMenuRow_1->removeAllChildrenWithCleanup(true);
            }
        }
        
        if(pmMenuRow_2 != NULL) {
            if (pmMenuRow_2->getChildrenCount() > 0) {
                pmMenuRow_2->removeAllChildrenWithCleanup(true);
            }
        }
        
        if(pmMenuRow_3 != NULL) {
            if (pmMenuRow_3->getChildrenCount() > 0) {
                pmMenuRow_3->removeAllChildrenWithCleanup(true);
            }
        }
        
        if(pmMenuRow_4 != NULL) {
            if (pmMenuRow_4->getChildrenCount() > 0) {
                pmMenuRow_4->removeAllChildrenWithCleanup(true);
            }
        }
        
    }

    void updateMoveCount() {
        char moveCountStr[1] = {0};
        sprintf(moveCountStr, "%d", pmMoveCount);
        pmLabelMoveCount->setString(moveCountStr);
    }

    bool checkNumberExists(int number) {
        bool retVal = false;
        int count = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                if (mMatrix[i][j] == number) {
                    count++;
                }
                if (count == 2) {
                    retVal = true;
                    break;
                }
            }
            if (retVal == true && count == 2) {
                break;
            }
        }
        return retVal;
    }
    // Methods
public:
    virtual bool init();
    static cc::CCScene *scene();
    
    CREATE_FUNC(GameScreen);
    
    ~GameScreen();
    void generateContent();
    void imageClicked(cc::CCMenuItemSprite *pSender);
    void backToMainScreen(cc::CCObject *pSender);
    void generateWinScreen();
    void newGame(cc::CCObject *pSender);
        
};

#endif /* defined(__match2__GameScreen__) */
